package com.zmj.controller;

import com.zmj.dto.TableData;
import com.zmj.entity.SysMenu;
import com.zmj.entity.SysUser;
import com.zmj.service.SysMenuService;
import com.zmj.utils.Response;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class SysMenuController {

    @Autowired
    private  SysMenuService sysMenuService;

    @RequestMapping("/sys/menu/list")
    public TableData findMenu (String search,String order,int limit,int offset){
        return  sysMenuService.findMenu(search,order,limit,offset);
    }
    @RequestMapping("/sys/menu/select")
    public Response selectTreeMenu(){

        List<SysMenu> list =  sysMenuService.findTreeMenu();
        return  Response.success().put("menuList",list);
    }

    //{orderNum: 0,parentId: 0,parentName: "一级目录",type: 1}
    @RequestMapping("/sys/menu/save")
    public  Response  saveMenu(@RequestBody SysMenu sysMenu){

        return  sysMenuService.save(sysMenu);
    }

    @RequestMapping("/sys/menu/info/{menuId}")
    public Response findById(@PathVariable long menuId){
        System.out.println("menuId"+menuId);
        return sysMenuService.findMenuById(menuId);
    }

    @RequestMapping("/sys/menu/update")
    public  Response update(@RequestBody SysMenu sysMenu){
        return  sysMenuService.update(sysMenu);
    }
    @RequestMapping("/sys/menu/del")
    public  Response del(@RequestBody List<Long> ids){
        return sysMenuService.del(ids);
    }


    @RequestMapping("/sys/menu/user")
    public  Response  menuList(HttpSession session){
        SysUser sysUser = (SysUser) session.getAttribute("sysUser");
       if (sysUser!=null){
         return    sysMenuService.findUserMenu(sysUser.getUserId());
       }
        System.out.println("sysUser:"+sysUser);
        return sysMenuService.findUserMenu(sysUser.getUserId());
    }
}
